<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <%@include file="../../../resources/inc/head.jsp" %>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="userName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="systemUserManage" lay-filter="systemUserManage"></table>
            <script type="text/html" id="optionToolbar">
                <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-sm" lay-event="add">新增  </button>
                        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="dels"id="dels">批量删除</button>

                </div>
            </script>
            <script type="text/html" id="optionBar">
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
            <script type="text/html" id="gmtModifiedTpl">
                {{#
                var date = new Date();
                date.setTime(d.gmtModified);
                return date.Format("yyyy-MM-dd hh:mm:ss");
                }}
            </script>
            <script type="text/html" id="isDisabledTpl">
                <input type="checkbox" name="lock" value="{{d.id}}" title="禁用" lay-filter="isDisabledFilter" {{
                       d.isLocked== 1 ? 'checked' : '' }}>
            </script>
            <script type="text/html" id="imgTpl">

                <div><img src="{{d.avatar==null?'<%=basePath%>resources/images/noImage.jpg':d.avatar}}" style="height: 28px" class="layui-table-link"></div>
            </script>
            <script type="text/html" id="sexTpl">
            {{d.sex==0?'女':'男'}}
            </script>
        </div>
    </div>
</div>

<%@include file="../../../resources/inc/footer.jsp" %>
<script>
    layui.config({
        base: '<%=basePath%>resources/lib/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;
        table.render({
            elem: '#systemUserManage',
            url: '<%=basePath%>manager/permission/user/list.action',
            toolbar: '#optionToolbar',
            cols: [[
                {type: 'checkbox',fixed:'left'},
                {field: 'id', width: 50, title: 'ID', sort: true}
                , {field: 'userName',width: 110, title: '用户名称'}
                , {field: 'realName', width: 110, title: '真实姓名'}
                , {field: 'phone', width: 190, title: '手机号码'}
                , {title: '头像', width:70,templet: '#imgTpl'}
                , {field: 'gmtModified', title: '更新时间', sort: true, templet: "#gmtModifiedTpl"}
                , {title: "操作", width: 310, align: "center", fixed: "right", toolbar: "#optionBar"}
            ]],
            page: !0,
            limit: 10,
            height: "full-130",
            text:{none:"暂无此数据"},
            response: {
                statusCode: 200
            }
        });
        //监听单元格编辑
        table.on('edit(systemUserManage)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
        });
        table.on('toolbar(systemUserManage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    var diglog=new UtilClass.LayerDialog({
                        title: '添加用户'
                        ,content: '<%=basePath%>manager/permission/user/create.action'
                    });
                    diglog.layerShow();
                    <%--layer.open({--%>
                        <%--type: 2--%>
                        <%--,title: '添加用户'--%>
                        <%--,content: '<%=basePath%>manager/permission/user/create.action'--%>
                        <%--,maxmin: true--%>
                        <%--,area: ['500px', '500px']--%>
                        <%--,btn: ['确定', '取消']--%>
                        <%--,yes: function(index, layero){--%>
                            <%--var iframeWindow = window['layui-layer-iframe'+ index];--%>
                            <%--var submitID = 'LAY-submit'--%>
                            <%--var submit = layero.find('iframe').contents().find('#'+ submitID);--%>
                            <%--//监听提交--%>
                            <%--iframeWindow.layui.form.on('submit('+ submitID +')', function(data){--%>
                                <%--var formData = data.field;--%>
                                <%--var b = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/g;--%>
                                <%--if(b.test(formData.password)){--%>
                                <%--layer.load(2);--%>
                                <%--formData.id=null;--%>
                                <%--$.ajax({--%>
                                    <%--url:"<%=basePath%>manager/permission/user/create.action",--%>
                                    <%--type:"POST",--%>
                                    <%--data:formData,--%>
                                    <%--dataType:"JSON",--%>
                                    <%--success:function (data) {--%>
                                        <%--layer.closeAll('loading');--%>
                                        <%--if(data.flag){--%>
                                            <%--table.reload('systemUserManage');--%>
                                            <%--layer.close(index);--%>
                                            <%--layer.msg("添加成功");--%>
                                            <%--layer.closeAll('loading');--%>
                                        <%--}else{--%>
                                            <%--layer.msg(data.msg, {icon: 5});--%>
                                        <%--}--%>
                                    <%--}--%>
                                <%--});}--%>
                                <%--else{layer.msg("密码必须包含数字与字母,且必须为八到十六位.")}--%>
                            <%--});--%>
                            <%--submit.trigger('click');--%>
                        <%--}--%>
                    <%--});--%>
                    break;
                case 'dels':
                    var data = checkStatus.data;
                    var List=JSON.stringify(data);
                    var jsList =JSON.parse(List);
                    if(jsList[0]==null){
                        layer.msg('请选中至少一行');

                    }else{


                        var chars=new Array();
                        for(var  li in jsList){
                            chars[li]=jsList[li].id;
                        }
                        var str="";
                        for( var i=0;i<chars.length;i++){
                            if(i==chars.length-1){
                                str+=chars[i];
                            }
                            else{str=str+chars[i]+",";}

                        }
                        layer.load(2);
                        $.ajax({
                            type: "get",
                            url: "<%=basePath%>manager/permission/user/delete/"+str+".action",
                            success: function(data) {
                                layer.closeAll("loading");
                                data=JSON.parse(data);


                                if(data.flag){
                                    table.reload('systemUserManage');

                                }else{
                                    layer.msg(data.msg, {icon: 5});
                                }
                            }
                        });
                        break;}
                    break;
                case 'permission':
                    var data = checkStatus.data;
                    var List=JSON.stringify(data);
                    var jsList =JSON.parse(List);
                    if(jsList.length!=1){
                        layer.msg('请选中一行');
                    }else{
                    layer.open({
                        type: 2
                        ,title: '添加权限'
                        ,content: '<%=basePath%>manager/permission/user/indexPermission.action'
                        ,maxmin: true
                        ,area: ['500px', '90%']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                        }
                    });}
                    break;
            };
        });
        table.on("tool(systemUserManage)", function (e) {

            if ("del" === e.event) {
                layer.confirm("真的删除行么", function (t) {
                    layer.close(t);

                    var List = JSON.stringify(e.data);
                    var jsList = JSON.parse(List);
                    var ids = jsList.id;
                    layer.load(2);
                    $.ajax({
                        type: "get",
                        url: "<%=basePath%>manager/permission/user/delete/" + ids + ".action",
                        success: function (data) {
                            layer.closeAll("loading");
                            data = JSON.parse(data);


                            if (data.flag) {
                                table.reload('systemUserManage');

                            } else {
                                layer.msg(data.msg, {icon: 5});
                            }
                        }
                    });

                })
            } else if ("edit" === e.event) {
                var diglog=new UtilClass.LayerDialog({
                    title: "编辑用户[" + e.data.userName + "]",
                    content: "<%=basePath%>manager/permission/user/update/" + e.data.id + ".action",
                });
                diglog.layerShow();
                // $(e.tr);
                <%--layer.open({--%>
                    <%--type: 2,--%>
                    <%--title: "编辑用户[" + e.data.userName + "]",--%>
                    <%--content: "<%=basePath%>manager/permission/user/update/" + e.data.id + ".action",--%>
                    <%--maxmin: !0,--%>
                    <%--area: ["500px", "500px"],--%>
                    <%--btn: ["确定", "取消"],--%>
                    <%--yes: function (index, layero) {--%>
                        <%--var iframeWindow = window['layui-layer-iframe' + index];--%>
                        <%--var submitID = 'LAY-submit'--%>
                        <%--var submit = layero.find('iframe').contents().find('#' + submitID);--%>
                        <%--iframeWindow.layui.form.on("submit(" + submitID + ")", function (data) {--%>
                            <%--var formData = data.field; //获取提交的字段--%>
                            <%--layer.load(2);--%>
                            <%--$.ajax({--%>
                                <%--url: "<%=basePath%>manager/permission/user/update.action",--%>
                                <%--type: "POST",--%>
                                <%--data: formData,--%>
                                <%--dataType: "JSON",--%>
                                <%--success: function (data) {--%>
                                    <%--layer.closeAll('loading');--%>
                                    <%--if (data.flag) {--%>
                                        <%--table.reload('systemUserManage');--%>
                                        <%--layer.close(index);--%>
                                        <%--layer.msg("编辑成功");--%>
                                        <%--layer.closeAll('loading');--%>
                                    <%--} else {--%>
                                        <%--layer.msg(data.msg, {icon: 5});--%>
                                    <%--}--%>
                                <%--}--%>
                            <%--});--%>
                        <%--});--%>
                        <%--submit.trigger("click")--%>
                    <%--},--%>
                    <%--success: function (e, t) {--%>
                    <%--}--%>
                <%--})--%>
            }
            else if ("userRole" === e.event){
                var data = e.data;
                 var id=data.id;
                layer.open({
                    type: 2
                    , title: '分配角色('+data.realName+')'
                    , content: '<%=basePath%>manager/permission/user/addRole/'+id+'.action'
                    , area: ['450px', '300px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index];
                        var submitID = 'LAY-submit'
                        var submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var formData = data.field;
                            selects=""+formData.tag2+"";
                            if(selects.length>0){
                            selects=selects.split(",")}
                            var selected=[];
                            var del=[];
                            var create=[];
                            var equality=[];
                            $.ajax({
                                type: "get",
                                url: "<%=basePath%>manager/permission/userRole/select/"+id+".action",
                                async:false,
                                success: function(data) {
                                    data=JSON.parse(data);
                                    for(var i=0;i<data.length;i++){
                                        selected.push(data[i].roleId);
                                    }
                                }
                            });

                            for(var i in selects){
                                for(var j in selected){
                                if(selects[i]==selected[j])
                                {equality.push(selects[i])}
                                }
                            }

                            for(var i in selected){
                                var flag=true;
                                for(var j in equality){
                                    if(selected[i]==equality[j])
                                    {flag=false;}
                                }
                                if(flag){
                                    del.push(selected[i]);
                                }
                            }
                            for(var i in selects){
                                var flag=true;
                                for(var j in equality){
                                    if(selects[i]==equality[j])
                                    {flag=false;
                                    }
                                }
                                if(flag){
                                    create.push(selects[i]);
                                }
                            }

                            if(del.length!=0){
                                for(var i in del){
                                    $.ajax({
                                        type: "get",
                                        url: "<%=basePath%>manager/permission/userRole/delete.action",
                                        data:{userId:id,roleId:del[i]},
                                        async:false,
                                        success: function(data) {
                                            data=JSON.parse(data);
                                            if(data.flag){
                                            }else{
                                                layer.msg(data.msg);
                                            }
                                        }
                                    });}
                            }
                            if(create[0]!=null){
                                for(var i in create){

                                $.ajax({
                                    type: "POST",
                                    url: "<%=basePath%>manager/permission/userRole/create.action",
                                    data:{userId:id,roleId:create[i]},
                                    async:false,
                                    success: function(data) {
                                        data=JSON.parse(data);
                                        if(data.flag){
                                        }else{
                                            layer.msg(data.msg);
                                        }
                                    }
                                });}
                            }
                            layer.close(index);
                            layer.msg("操作完成");
                        });
                        submit.trigger('click');
                    }
                });

        }

        });
        //监听锁定
        form.on('checkbox(isDisabledFilter)', function (obj) {
            var id=this.value;
            $.ajax({
                url:"<%=basePath%>manager/permission/user/update.action",
                type:"POST",
                data:{id:id,isLocked:obj.elem.checked==true?1:0},

                success:function (data) {

                    layer.closeAll('loading');
                    data=JSON.parse(data);

                    if(data.success){
                        table.reload('systemUserManage');

                    }else{
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });
        });
        //监听搜索
        form.on('submit(LAY-user-front-search)', function (data) {
            var field = data.field;
            $(".layui-laypage-skip").find("input").val("1");
            $(".layui-laypage-btn").click();
            table.reload('systemUserManage', {

                where: field

            });
        });


    });
    function edit(e) {

    }

</script>
</body>
</html>

